package cz.anime.ppro.beans;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.anime.ppro.enums.UserRolesEnum;
import cz.anime.ppro.logic.CommentComponent;
import cz.anime.ppro.logic.ContentComponent;
import cz.anime.ppro.logic.UserComponent;
import cz.anime.ppro.model.Comment;
import cz.anime.ppro.model.Content;

@Component
@ManagedBean
@Scope("view")
public class ContentBean {

	private int contentId;
	
	private boolean admin = false;
	
	private String commentTitle;
	private String commentText;
	
	private Content content;
	
	private List<Comment> comments;

	@Autowired
	private ContentComponent contentComponent;
	
	@Autowired
	private CommentComponent commentComponent;
	
	@Autowired
	private  UserComponent userComponent;
	
	@PostConstruct
	public void init(){
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		setContentId(Integer.parseInt(req.getParameter("content")));
		if (contentId != 0) {
			setContent(contentComponent.getContent(contentId));
			setComments(commentComponent.getCommentsByContentId(contentId));
		}
		if (userComponent.getUser(getCurrentUser()).getRole().equals(UserRolesEnum.ROLE_ADMIN.name())) {
			admin = true;
		}
	}
	
	public void deleteComment(int commentId){	
		Comment comment = commentComponent.getCommentById(commentId);
		
		commentComponent.deleteComment(comment);
		getComments().remove(comment);
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/PPRO2/content.jsf?content="+contentId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void createComment(int contId){	
		Comment comment = new Comment();
		
		comment.setDate(Calendar.getInstance().getTime());
		comment.setContent(contentComponent.getContent(contId));
		comment.setText(getCommentText());
		comment.setTitle(getCommentTitle());
		comment.setWebuser(userComponent.getUser(getCurrentUser()));
		
		commentComponent.createComment(comment);
		getComments().add(comment);
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/PPRO2/content.jsf?content="+contId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private int getCurrentUser(){
		return 2;
	}
	
	public Content getContent() {
		return content;
	}

	public void setContent(Content content) {
		this.content = content;
	}

	public int getContentId() {
		return contentId;
	}

	public void setContentId(int contentId) {
		this.contentId = contentId;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}

	public String getCommentTitle() {
		return commentTitle;
	}

	public void setCommentTitle(String commentTitle) {
		this.commentTitle = commentTitle;
	}

	public String getCommentText() {
		return commentText;
	}

	public void setCommentText(String commentText) {
		this.commentText = commentText;
	}
	
}
